package com.zhuss.controller.core;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.zhuss.common.AbstractBo;
import com.zhuss.common.BaseController;
import com.zhuss.common.StartSqlBo;
import com.zhuss.core.db.dao.ext.ConfigDao;
import com.zhuss.core.processor.StartRead;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;


/**
 * 下拉框 controller
 * @Author zhushanshuo
   @Date 2019-11-27 19:58
 *
 */
@RestController
@RequestMapping("/start")
public class StartController extends BaseController {
	
	Logger  LOGGER = LoggerFactory.getLogger(StartController.class);
	
	@Autowired
	private ConfigDao configDao;
	
	@Autowired
	private StartRead startRead;

	/**
	 * 重启
	 * @return
	 * @throws DzcxException
	 */
	@ResponseBody
	@RequestMapping("/restart")
	public void restart(@RequestBody AbstractBo bo) throws DzcxException {
		Long userId = bo.getConcurrentUserId();
		if (userId.longValue() != 7l) {
			throw new DzcxException(ExceptionCode.非法人类);
		}
		
		 String cmd ="sudo /home/wiseks/restart.sh";
		 new Thread(new Runnable() {
			@Override
			public void run() {
				Process obj = null;
		         try {
		        	Thread.sleep(1000);
		            obj = Runtime.getRuntime().exec(cmd);
		            int status = obj.waitFor();
		            LOGGER.error("status: " + status);
		            LOGGER.error(JsonUtil.toJson(obj));
		         } catch (Exception e) {
		        	e.printStackTrace();
		        	LOGGER.error(JsonUtil.toJson(obj));
		         }
			}
		 }).start();
	}
	
	/**
	 * 上线
	 * @return
	 * @throws DzcxException
	 */
	@ResponseBody
	@RequestMapping("/online")
	public void online(@RequestBody AbstractBo bo) throws DzcxException {
		
		Long userId = bo.getConcurrentUserId();
		if (userId.longValue() != 7l) {
			throw new DzcxException(ExceptionCode.非法人类);
		}
		
		
		String cmd ="sudo /home/wiseks/online.sh";
		
		new Thread(new Runnable() {
			@Override
			public void run() {
				Process obj = null;
				try {
					Thread.sleep(1000);
					obj = Runtime.getRuntime().exec(cmd);
					int status = obj.waitFor();
		            LOGGER.error("status: " + status);
		            LOGGER.error(JsonUtil.toJson(obj));
				} catch (Exception e) {
					e.printStackTrace();
					LOGGER.error(JsonUtil.toJson(obj));
				}
			}
		 }).start();
	}
	
	/**
	 * sql
	 * @return
	 * @throws DzcxException
	 */
	@ResponseBody
	@RequestMapping("/startSql")
	public Object startSql(@RequestBody StartSqlBo bo) throws DzcxException {
		
		Long userId = bo.getConcurrentUserId();
		if (userId.longValue() != 7l) {
			throw new DzcxException(ExceptionCode.非法人类);
		}
		
		String sql = bo.getSql();
		
		String sqlBig = sql.toLowerCase();
		if (sqlBig.contains("drop") || sqlBig.contains("truncate") || sqlBig.contains("delete ")) {
			throw new DzcxException(ExceptionCode.非法SQL);
		}
		
		Object result = null;
		
		if (sqlBig.contains("select ")) {
			result = startRead.startSql(bo);
			return result;
		}
		if (sqlBig.contains("insert ")) {
			result = configDao.startInsertSql(bo);
			return result;
		}
		if (sqlBig.contains("update ")) {
			result = configDao.startUpdateSql(bo);
			return result;
		}
		
		if (!sqlBig.contains("alter ") || !sqlBig.contains("create ")) {
			throw new DzcxException(ExceptionCode.非法SQL);
		}
		
		return result;
	}

}

